# do all flickrapi overrides here...
import flickrapi, math
import xml.etree.ElementTree as ElementTree

sets_information = [
    { 'id' : 1, 'photos' : 1, 'videos' : 0 },
    { 'id' : 2, 'photos' : 0, 'videos' : 0 },
    { 'id' : 3, 'photos' : 10000, 'videos' : 300 },
    { 'id' : 4, 'photos' : 499, 'videos' : 0 },
    { 'id' : 5, 'photos' : 500, 'videos' : 0 },
    { 'id' : 6, 'photos' : 501, 'videos' : 0 }
]

def get_set_info(id):
    for s in sets_information:
        if s['id'] == int(id):
            return s
    return None

photoset_result = """
<photoset id="%(id)i" primary="primary%(id)i" secret="secret%(id)i"
	server="server%(id)i" photos="%(photos)i" videos="%(videos)i" farm="farm%(id)i">
	<title>Test %(id)i</title>
	<description>Test %(id)i description</description>
</photoset>
"""
photo_result = """<photo 
id="%(id)s" secret="secret%(id)s" server="server%(id)s" 
farm="farm%(id)s" title="Title %(id)s" isprimary="1" 
originalsecret="originalsecret%(id)s" originalformat="jpg" 
lastupdate="1232549345" media="%(type)s" media_status="ready"/>
"""

class FakeFlickrAPI(object):
    
    def __init__(self, key, secret):
        self.key = key
        self.secret = secret
        self.retrieved_photos = {}

    def photosets_getList(self):

        return ElementTree.fromstring("""<rsp stat="ok"><photosets cancreate="1">""" +
            ''.join([photoset_result % set_info for set_info in sets_information]) +
        """</photosets></rsp>""")

    def auth_checkToken(self, *args, **kwargs):
        return ElementTree.fromstring("""
    <auth>
    	<token>976598454353455</token>
    	<perms>read</perms>
    	<user nsid="12037949754@N01" username="Bees" fullname="Cal H" />
    </auth>""")

    def auth_getFrob(self, *args, **kwargs):
        return ElementTree.fromstring("""<frob>746563215463214621</frob>""")

    def auth_getToken(self, *args, **kwargs):
        return ElementTree.fromstring("""
    <auth>
    	<token>976598454353455</token>
    	<perms>write</perms>
    	<user nsid="12037949754@N01" username="Bees" fullname="Cal H" />
    </auth>""")

    def photosets_getPhotos(self, photoset_id='',per_page=500,extras='',page=1,media='all'):
        if not self.retrieved_photos.has_key(photoset_id):
            self.retrieved_photos[photoset_id] = {'photos' : 0, 'videos' : 0}
        
        set_info = get_set_info(photoset_id)
        total = (set_info['photos'] + set_info['videos'])
        count_info = self.retrieved_photos[photoset_id]
        
        head = """<rsp stat="ok">
    <photoset id="%(id)s" primary="2483" page="%(page)i" 
        perpage="%(per_page)i" pages="%(pages)i" total="%(total)i">""" % {
            'id' : photoset_id,
            'page' : page,
            'per_page' : per_page,
            'pages' : int(math.ceil(float(total)/float(500))),
            'total' : total
        }
        
        photos_rsp = ''
        
        current_count = 0

        while current_count < per_page and (count_info['photos'] + count_info['videos']) < total:
            current_count += 1
            if count_info['photos'] < set_info['photos']:
                photos_rsp += photo_result % {'id' : str(page) + str(current_count), 'type' : 'photo'}
                count_info['photos'] += 1
            else:
                photos_rsp += photo_result % {'id' : str(page) + str(current_count), 'type' : 'video'}
                count_info['videos'] += 1
        
        return ElementTree.fromstring(head + photos_rsp + """</photoset></rsp>""")
    
flickrapi.FlickrAPI = FakeFlickrAPI
    
from flickrmirror.view.base import IView
from flickrmirror.controller import MirrorController

class TestView(IView):
    name = "test view"
    
    def __init__(self):
        self.controller = MirrorController(self)
        self.last_prompt = None
        self.last_status = None 
        self.next_prompt_result = None
        self.last_log = None
         
    def prompt(self, msg, result=True):
        self.last_prompt = msg
        if result:
            return self.next_prompt_result
        
    def status(self, msg):
        self.last_status = msg
        
    def log(self, msg):
        self.last_log = msg
        


